# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models, api

import logging
_logger = logging.getLogger(__name__)


class IotGateway(models.Model):
    _name = "iot.gateway"
    _inherit = ['iot.gateway', 'iot.mqtt.interface']

    def action_put_config(self, value={}):
        self.ensure_one()

        values = {
            'name': self.name,
            'latitude': self.latitude,
            'longitude':self.longitude,
            'router_code': self.router_code,
            'sensor_ids':[]
        }

        self.publish( 'put', values= values, userdata={'function': 'config'} )

    def action_get_config(self):
        self.ensure_one()
        self.publish( 'get', userdata={'function': 'config'} )

    @api.model
    def on_message_get_ack(self, code, userdata=None, result=None, *args, **kwargs):
        print('on_message_get_ack gateway: ', code, userdata, result, args, kwargs)
        if userdata.get( 'function' ) == 'config':
            print('on_message_get_ack : config, router', result )

    @api.model
    def on_message_put_ack(self, code, userdata=None, result=None, *args, **kwargs):
        print('on_message_put_ack gateway: ', code, userdata, result, args, kwargs)
        if userdata.get( 'function' ) == 'config':
            print('on_message_put_ack : config, router', result and 'ok' or 'error' )


    # def action_regack(self, msg=None):
    #     self.ensure_one()
    #     if not msg:
    #         msg = {'result': 'reg ok'}
    #     self.action_publish('ackput', 'register', msg)

    #     # if method == 'put' and func == 'register':
    #     #     obj = self.create({'code': code})
    #     #     obj.action_regack()


    # #     if method == 'put' and func == 'register':
    # #         self.action_regack( {'result': 'reg again'} )


    # #     if method == 'put' and func == 'measure':
    # #         print(method, func, self.code, kwargs)


